package io.xxx.mario.task.talk;

import io.xxx.mario.robot.Robot;
import io.xxx.mario.task.Task;
import io.xxx.mario.task.TaskMessage;
import io.xxx.mario.task.receive.Receiver;

import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

public abstract class GenericTalker implements Talker, Runnable {

    protected final BlockingDeque<Context> queue = new LinkedBlockingDeque<>(200);

    protected Robot robot;

    public GenericTalker(Robot robot) {
        this.robot = robot;
    }

    @Override
    public void say(Task task, Receiver receiver, List<TaskMessage> messages) throws InterruptedException {
        queue.put(new Context(task, receiver, messages));
    }

    @Override
    public void run() {
        try {
            Context context = queue.take();
            push(context);
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    protected abstract void push(Context context);
}
